import { Router } from 'vue-router'
import { getToken } from '@/utils/cookie'
import { useAppStore } from '@/store/modules/app'
import { ViewLogin, ViewHome } from '@/enums'

export const setupRouterGuard = async (router: Router) => {
    if(getToken() && !useAppStore().init){
        await useAppStore().Init()
    }
    router.beforeEach(async (to) => {
        const token = getToken()
        if(to.fullPath === ViewLogin.Path) return
        if(!token) return { path: '/login' }
        if(token && !useAppStore().init){
            await useAppStore().Init()
            return ViewHome.Path
        }
    })
}
